package cn.rdq.iknesssystem.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.rdq.api.bo.DashBoardBo;
import cn.rdq.api.po.Views;
import cn.rdq.common.constant.RedisConst;
import cn.rdq.common.util.RedisUtil;
import cn.rdq.iknesssystem.service.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DashBoardServiceImpl implements DashBoardService {

    @Autowired
    private UserService userService;

    @Autowired
    private DramaService dramaService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private RedisUtil redisUtil;

    @Value("${ikness-platform.url}")
    public String url;

    @Autowired
    private ViewsService viewsService;

    @Override
    public DashBoardBo dashboard() {
        DashBoardBo dashBoardBo = new DashBoardBo();
        int userCount = userService.count();
        dashBoardBo.setUserCount(userCount);
        int dramaCount = dramaService.count();
        dashBoardBo.setDramaCount(dramaCount);
        int blogCount = blogService.count();
        dashBoardBo.setBlogCount(blogCount);
//        String result = HttpUtil.get(url + "/status/online");
//        JSONObject resJson = JSON.parseObject(result);
//        Integer onlineCount = resJson.getJSONObject("data").getInteger("online");
        Object onlineCount = redisUtil.get(RedisConst.ONLINE_KEY);
        dashBoardBo.setOnlineCount((Integer) onlineCount);

        List<Views> viewsList = viewsService.getRecentViews(30);
        dashBoardBo.setViewsData(viewsList);
        return dashBoardBo;
    }
}
